package com.uenpay.im.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qyx.android.file.Logger;
import com.qyx.android.weight.utils.QyxWeightDensityUtils;
import com.qyx.android.weight.view.MyListView;
import com.uenpay.im.R;
import com.uenpay.im.callback.IPopupWindowListener;
import com.uenpay.im.entities.PopupEntity;

import java.util.ArrayList;

public class MyPopupWindow extends PopupWindow {
	private IPopupWindowListener iPopupWindowListener;
	private Context mContext;
	private Rect mRect = new Rect();
	private final int[] mLocation = new int[2];
	private int popupGravity = Gravity.NO_GRAVITY;
	private ArrayList<PopupEntity> arrayList = new ArrayList<PopupEntity>();
	private LayoutInflater layoutInflater = null;

	/**
	 * 
	 * @param context
	 * @param _IPopupWindowListener
	 */
	public MyPopupWindow(Context context, ArrayList<PopupEntity> _arrayList, IPopupWindowListener _IPopupWindowListener) {
		super();
		this.iPopupWindowListener = _IPopupWindowListener;
		this.mContext = context;
		arrayList = _arrayList;
		layoutInflater = LayoutInflater.from(context);
		// 设置可以获得焦点
		setFocusable(true);
		// 设置弹窗内可点击
		setTouchable(true);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new BitmapDrawable());
		// 设置弹窗外可点击
		setOutsideTouchable(true);
		// 设置弹窗的宽度和高度
		// 设置弹窗的布局界面
		setContentView(LayoutInflater.from(mContext).inflate(R.layout.layout_relations_pop, null));
		initUI();
	}

	private void initUI() {
		PopAdapter mAdapter = new PopAdapter();
		int width = QyxWeightDensityUtils.getScreenWidth((Activity) mContext);

		MyListView listview = (MyListView) getContentView().findViewById(R.id.pop_list);
		LayoutParams lp = listview.getLayoutParams();
		lp.width = width / 3 - 50;
		lp.height = LayoutParams.WRAP_CONTENT;
		listview.setLayoutParams(lp);
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				PopupEntity popupEntity = (PopupEntity) arg0.getAdapter().getItem(arg2);
				if (popupEntity != null) {
					iPopupWindowListener.itemClick(popupEntity.type);
					dismiss();
				}
			}
		});
	}

	/**
	 * 显示弹窗列表界面
	 */
	public void show(View view) {
		//设置背景透明度
		WindowManager.LayoutParams lp = ((Activity)mContext).getWindow().getAttributes();
		lp.alpha = 0.7f;
		((Activity)mContext).getWindow().setAttributes(lp);
		view.getLocationOnScreen(mLocation);
		mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(), mLocation[1] + 2*view.getHeight());
		showAtLocation(view, popupGravity, mLocation[0] - QyxWeightDensityUtils.dp2px(mContext, 110.0f) + 50, mRect.bottom
				+ QyxWeightDensityUtils.dp2px(mContext, -7.0f));
	}

	private class PopAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return arrayList == null ? 0 : arrayList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arrayList == null ? null : arrayList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			PopupEntity popupEntity = arrayList.get(arg0);
			PopViewHodler holder = null;
			if (arg1 == null) {
				holder = new PopViewHodler();
				arg1 = layoutInflater.inflate(R.layout.item_pop_layout, null);
				holder.pop_tv = (TextView) arg1.findViewById(R.id.pop_tv);
				holder.pop_iv = (ImageView) arg1.findViewById(R.id.pop_iv);
				arg1.setTag(holder);
			} else {
				holder = (PopViewHodler) arg1.getTag();
			}
			holder.pop_iv.setImageResource(popupEntity.resourcesIvId);
			holder.pop_tv.setText(popupEntity.resourcesTvId);
			return arg1;
		}

		private class PopViewHodler {
			private TextView pop_tv;
			private ImageView pop_iv;
		}
	}

	@Override
	public void dismiss() {
		super.dismiss();
		//设置背景透明度
		WindowManager.LayoutParams lp = ((Activity)mContext).getWindow().getAttributes();
		lp.alpha = 1.0f;
		((Activity)mContext).getWindow().setAttributes(lp);
	}
}

